// 搜索下拉
$choosed = $(".choosed");
$choosed.on("click", function () {
    $(".choicebox").css({
        display: "block"      
    });     
    return false;
})
$choicebox = $(".choicebox");
var choosed = document.querySelector(".choosed");
$(".choices").on("click",function(evt){
    var e = evt ||event;
    var target = e.target || e.srcElement;
    // console.log(target);
    choosed.innerHTML = target.innerHTML +"<i class='fa fa-caret-down'></i>";
    
    // console.log(choosed);
    $choicebox.css({
        display:"none"
    })
})
//  吸顶菜单
var $header = $("#header");

$(window).scroll(function(){
    var scrollTop = $("html,bady").scrollTop();
    if(scrollTop > 80){
        $header.addClass("header_anm");
        $(".header-content2").css({
            display:"block",
            top:-8
        })
        $(".CarShop").css({
            display:"none"
        })
        $(".nav").css({
            top:50
        })
        $(".header-bottom").css({
            top:0,
            backgroundColor:"#fff"          
        })
        $(".header-bottom a").css({
            color:"#000"
        })
    }else{
        $header.removeClass("header_anm");
        $(".header-content2").css({
            display:"none",
            
        })
        $(".CarShop").css({
            display:"block"
        })
        $(".nav").css({
            top:80
        })
        $(".header-bottom").css({
            top:80,
            backgroundColor:"#1f1f1f"          
        })
        $(".header-bottom a").css({
            color:"#fff"
        })
    }
})
$(window).trigger("scroll");

// 购物车渲染
var cart_list = (function(){
    var storage = localStorage.getItem("cart_list");
    if(storage === null){
          return [];
    }
    return JSON.parse(storage);
})();
console.log(cart_list);
function render(){
    var html = "";
    // 计算总价
    var total_price = 0;
    var total_buyable = 0
    for(var i=0;i<cart_list.length;i++){
        // 价格
        var price = cart_list[i].favorite_count;
        console.log(price);
        // 数量
        var buyable = cart_list[i].buyable;
        total_buyable += buyable;
        console.log(buyable);
        // 单个总价
        var item_price = price * buyable;
        total_price += item_price;
        // 数据拼接
        html +=`<li>
        <div class="item">
            <div class="item clearfix" style="display: block;">
                <div class="f_l choice">
                    <input type="checkbox" name="quanxuan" id="qx">                                      
                </div>
                <div class="f_l infobox">
                    <div class="f_l thumb">
                        <a href="#">
                            <img src="${cart_list[i].photo.path}" alt="">                  
                        </a>
                    </div>
                    <div class="f_l detail">
                        <div class="title" >${cart_list[i].favorite_count}</div>
                        <p class="PC_name">
                            <a href="#">${cart_list[i].sender.username}</a>
                        </p>
                        <p class="p1">34.0x44.0x8.0cm</p>
                        <p class="shuliang _hidden">
                            <span>${buyable}</span>件
                        </p>
                        <p class="p2">${cart_list[i].add_datetime}</p>
                    </div>
                    <div class="f_l shanchu">删除</div>
                    <div class="f_l price_unit">
                        <p class="now price" style="display:inline-block">
                            ¥ <span class="dj">${price}</span></br>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </li>`

    }
    $(".buy_cart_list ul").html(html);
    $(".qian span").html(total_price);
    $(".queren span").html(total_buyable);
    if(total_buyable>0){
        $(".queren").css({
            backgroundColor:"Orange"
        })
    }
}
render();
